#include "colorfabric.h"
#include <time.h>

ColorFabric::ColorFabric()
{
    _baseColors.push_back(QColor(140, 30, 30));
    _baseColors.push_back(QColor(128, 142, 30));
    _baseColors.push_back(QColor(30, 142, 135));
    _baseColors.push_back(QColor(49, 30, 142));
    _baseColors.push_back(QColor(135, 30, 142));

    srand(time(NULL));
}

QSharedPointer<ColorFabric> ColorFabric::_instance;

QSharedPointer<ColorFabric> ColorFabric::instance()
{
    if(_instance.isNull())
    {
        _instance = QSharedPointer<ColorFabric>(new ColorFabric);
    }
    return _instance;
}

QColor ColorFabric::createColor()
{
    int colorIndex = (rand()*(_baseColors.size()-2))/RAND_MAX;
    double randArg = (rand()*1.0)/RAND_MAX;

    int rStep = randArg*(_baseColors[colorIndex+1].red() - _baseColors[colorIndex].red());
    int gStep = randArg*(_baseColors[colorIndex+1].green() - _baseColors[colorIndex].green());
    int bStep = randArg*(_baseColors[colorIndex+1].blue() - _baseColors[colorIndex].blue());


    return QColor(_baseColors[colorIndex].red() + rStep,
                  _baseColors[colorIndex].green() + gStep,
                  _baseColors[colorIndex].blue() + bStep);
}
